package com.ibeeking.found.content.c.rest.service.impl;

import com.ibeeking.found.content.c.rest.service.IBroadcastGroupService;
import com.ibeeking.found.content.c.rest.service.IBroadcastService;
import com.ibeeking.found.content.service.common.dos.BroadcastGroupDO;
import com.ibeeking.found.content.service.common.param.BroadcastGroupParam;
import com.ibeeking.found.content.service.common.query.BroadcastGroupPageQuery;
import com.ibeeking.found.content.service.common.query.BroadcastGroupQuery;
import com.ibeeking.found.content.service.common.vo.BroadcastCount;
import com.ibeeking.found.content.service.common.vo.BroadcastGroupVO;
import com.ibeeking.found.content.service.mapper.BroadcastGroupMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName BroadcastGroupServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2022-02-18 14:14
 **/
@Service
public class BroadcastGroupServiceImpl extends ServiceImpl<BroadcastGroupMapper, BroadcastGroupDO> implements IBroadcastGroupService {

    @Resource
    private BroadcastGroupMapper broadcastGroupMapper;
    @Resource
    private IBroadcastService broadcastServiceImpl;

    @Override
    public List<BroadcastGroupVO> list(BroadcastGroupQuery broadcastGroupQuery) {
        List<BroadcastGroupDO> broadcastGroupDOs = broadcastGroupMapper.selectList(new LambdaQueryWrapper<BroadcastGroupDO>()
                .eq(broadcastGroupQuery.getId() != null, BroadcastGroupDO::getId, broadcastGroupQuery.getId())
                .in(Collections3.isNotEmpty(broadcastGroupQuery.getIds()), BroadcastGroupDO::getId, broadcastGroupQuery.getIds())
                .like(StringUtils.isNotBlank(broadcastGroupQuery.getName()), BroadcastGroupDO::getName, broadcastGroupQuery.getName())
                .orderByDesc(BroadcastGroupDO::getCreateTime)
        );
        return JsonUtils.jsonToList(broadcastGroupDOs, BroadcastGroupVO.class);
    }

    @Override
    public Page<BroadcastGroupVO> pageList(BroadcastGroupPageQuery broadcastGroupPageQuery) {
        Page<BroadcastGroupDO> pages = broadcastGroupMapper.selectPage(new Page<BroadcastGroupDO>(broadcastGroupPageQuery.getPageNum(), broadcastGroupPageQuery.getPageSize()), new LambdaQueryWrapper<BroadcastGroupDO>().like(StringUtils.isNotBlank(broadcastGroupPageQuery.getName()), BroadcastGroupDO::getName, broadcastGroupPageQuery.getName()));
        Page<BroadcastGroupVO> broadcastGroupVOPage = BeanUtil.convertPage(pages, BroadcastGroupVO.class);
        List<BroadcastGroupVO> records = broadcastGroupVOPage.getRecords();
        if (Collections3.isNotEmpty(records)) {
            //查询直播间数
            List<BroadcastCount> broadcastCounts = broadcastServiceImpl.countByGroup(records.stream().map(BroadcastGroupVO::getId).collect(Collectors.toList()));
            Map<Long, Integer> broadcastCountMap = broadcastCounts.stream().collect(Collectors.toMap(BroadcastCount::getId, BroadcastCount::getNum));
            records.forEach(x -> x.setBroadcastNum(broadcastCountMap.get(x.getId())));
        }
        return broadcastGroupVOPage;
    }

    @Override
    public BroadcastGroupVO view(Long id) {
        BroadcastGroupDO broadcastGroupDO = broadcastGroupMapper.selectById(id);
        return BeanUtil.convertBean(broadcastGroupDO, BroadcastGroupVO.class);
    }

    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(BroadcastGroupParam param) {
        LambdaQueryWrapper<BroadcastGroupDO> queryWrapper = new LambdaQueryWrapper<BroadcastGroupDO>()
                .ne(param.getId() != null, BroadcastGroupDO::getId, param.getId())
                .eq(StringUtils.isNotBlank(param.getName()), BroadcastGroupDO::getName, param.getName());
        List<BroadcastGroupDO> dos = broadcastGroupMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在相同名称的分组!");
        }
    }
}
